<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\VServerOverdueList;
use Illuminate\Support\Facades\Log;

class sendNotice extends Command
{
    /**
     * wsdl 地址
     */
    const URL = 'http://9.23.16.139:30008/wechatBase-service/services/ExternalMessageService?wsdl';

    /**
     * 账号信息
     */
    const SYS_ID = 'OkIT';

    /**
     * 账号信息
     */
    const SYS_ID_PASS = 'OKIT9999';

    /**
     * 账号信息
     */
    const SYS_FLAG = 'OKIT0000';

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'send:notice';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '推送逾期消息';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     * @throws \SoapFault
     */
    public function handle()
    {

        try {
            $service = new \SoapClient(self::URL,[
                'trace' => true
            ]);

            $data = VServerOverdueList::with([
                'user' => function($query){
                    return $query->with('person');
                }
            ])
                ->whereIn('typeOne',[
                    '强制要求',
                    '市场拓展'
                ])
                ->whereIn('overdue_days',[
                    7,15
                ])
                ->select([
                    'project_name',
                    'user_id',
                    'overdue_days',
                    'number'
                ])
                ->get()
                ->toArray();


            foreach ($data as $value) {
                $content = "服务单名称：". $value['project_name']."<br/>" ." 编号：".$value['number']."<br/>逾期 ".$value['overdue_days'] . "天，请及时审批";
                $mobile =  $value['user']['person']['mobile'] ?? '';

                $parameters = [
                    'in' => [
                        'sys_id' => self::SYS_ID,
                        'sys_id_pass' => self::SYS_ID_PASS,
                        'sys_flag' => self::SYS_FLAG,
                        'userType' => '2',
                        'userNum'  => '110104198308083010',
                        'content'  => $content
                    ]
                ];

                $result = $service->sendTextMsg($parameters);

                Log::channel('message')
                    ->info('消息推送成功，推送人员:'.$value['mobile'] .' 推送内容：'.$content,json_encode($result));
            }
        }catch (\Exception|\Throwable $exception){
            Log::channel('message')
                ->info('消息推送失败，异常内容：'.$exception->getMessage());
        }
    }
}
